<?php
/**
 * Mysql
 * 
 * @package
 * @author forum
 * @copyright chzcb
 * @version 2009
 * @access public
 */
class Mysql {
	public $currentdb = "";
	function __construct() {
		mysql_connect ( DBHOST, DBUSER, DBPASSWD ) or die ( "Could not connect: " . mysql_error () );
	}
	protected function sql_selectdb($dbname) {
		if (DBNAME != $dbname && ! empty ( $dbname )) {
			define ( 'DBNAME', $dbname );
		}
		mysql_select_db ( DBNAME );
		mysql_query ( "SET character_set_connection=utf8, character_set_results=utf8, character_set_client=binary" );
	}
	function sql_insert($sql, $dbname = null) {
		$this->sql_selectdb ( $dbname );
		mysql_query ( $sql );
		return mysql_insert_id ();
	}
	function sql_query($sql, $dbname = null) {
		$this->sql_selectdb ( $dbname );
		mysql_query ( $sql );
	}
	function sql_fetch_one($sql, $dbname = null) {
		$this->sql_selectdb ( $dbname );
		$r = mysql_query ( $sql );
		if ((! empty ( $r )) && ($row = mysql_fetch_array ( $r, MYSQL_ASSOC ))) {
			return $row;
		} else {
			return mysql_error ();
		}
		return 0;
	}
	
	function sql_fetch_one_cell($sql, $dbname = null) {
		$this->sql_selectdb ( $dbname );
		$r = mysql_query ( $sql );
		if ((! empty ( $r )) && ($row = mysql_fetch_array ( $r, MYSQL_NUM ))) {
			return $row [0];
		} else {
			return mysql_error ();
		}
		return 0;
	}
	function sql_fetch_rows($sql, $dbname = null) {
		$this->sql_selectdb ( $dbname );
		$r = mysql_query ( $sql );
		
		$ret = array ();
		if (! empty ( $r )) {
			while ( $row = mysql_fetch_array ( $r, MYSQL_ASSOC ) ) {
				$ret [] = $row;
			}
		} else {
			return mysql_error ();
		}
		return $ret;
	}
	function sql_fetch_num_rows($sql, $dbname = null) {
		$this->sql_selectdb ( $dbname );
		$Result = mysql_query ( $sql );
		$ret = mysql_num_rows ( $Result );
		return $ret;
	}
}

?>